package services

import (
	"iot-cloud-platform/internal/auth"
	"iot-cloud-platform/internal/config"
	"iot-cloud-platform/internal/database"
	"iot-cloud-platform/internal/services/device"
	"iot-cloud-platform/internal/services/telemetry"

	"github.com/redis/go-redis/v9"
	"gorm.io/gorm"
)

// Container holds all service instances
type Container struct {
	Config        *config.Config
	DB            *gorm.DB
	Redis         *redis.Client
	RedisManager  *database.RedisManager
	InfluxManager *database.InfluxManager

	// Services
	AuthService      *auth.AuthService
	DeviceService    *device.DeviceService
	TelemetryService *telemetry.TelemetryService

	// Controllers
	AuthController      *auth.AuthController
	DeviceController    *device.DeviceController
	TelemetryController *telemetry.TelemetryController
}

// NewContainer creates a new service container
func NewContainer(db *gorm.DB, redisClient *redis.Client, influxManager *database.InfluxManager, cfg *config.Config) *Container {
	redisManager := database.NewRedisManager(redisClient)

	// Initialize services
	authService := auth.NewAuthService(db, redisManager, cfg)
	deviceService := device.NewDeviceService(db, redisManager)
	telemetryService := telemetry.NewTelemetryService(db, influxManager, redisManager)

	// Initialize controllers
	authController := auth.NewAuthController(authService)
	deviceController := device.NewDeviceController(deviceService)
	telemetryController := telemetry.NewTelemetryController(telemetryService)

	return &Container{
		Config:        cfg,
		DB:            db,
		Redis:         redisClient,
		RedisManager:  redisManager,
		InfluxManager: influxManager,

		// Services
		AuthService:      authService,
		DeviceService:    deviceService,
		TelemetryService: telemetryService,

		// Controllers
		AuthController:      authController,
		DeviceController:    deviceController,
		TelemetryController: telemetryController,
	}
}
